package control.admin;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class CoolServlet extends HttpServlet {

    protected String error = null;
    protected String successForm = null;
    protected String createProductServlet = null;
    protected String createClientServlet = null;
    protected String deleteProductServlet = null;
    protected String editProductServlet = null;
    protected String readProductServlet = null;
    protected String persistenceMechanism = null;
    protected String loginServlet = null;
    protected String administration = null;
    protected String informacionesProducts = null;
    protected String clientview = null;
    protected String productChanged = null;
    protected String readDetailsProductServlet = null;
    protected String informacionesDetalladasProducts = null;
    protected String addProductToCartServlet = null;
    protected String loginform = null;
    

    protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException;

    protected void gotoNamedResource(String address, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getNamedDispatcher(address);
        dispatcher.forward(request, response);
    }

    protected void gotoURL(String address, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    @Override
    public void init() {
        ServletConfig config = getServletConfig();
        ServletContext context = config.getServletContext();
        error = context.getInitParameter("error");
        successForm = context.getInitParameter("successForm");
        createProductServlet = context.getInitParameter("createProductServlet");
        createClientServlet = context.getInitParameter("createClientServlet");
        deleteProductServlet = context.getInitParameter("deleteProductServlet");
        readProductServlet = context.getInitParameter("readProductServlet");
        editProductServlet = context.getInitParameter("editProductServlet");
        persistenceMechanism = context.getInitParameter("persistenceMechanism");
        loginServlet = context.getInitParameter("loginServlet");
        administration = context.getInitParameter("administrationview");
        informacionesProducts = context.getInitParameter("informacionesProducts");
        clientview = context.getInitParameter("clientview");
        productChanged = context.getInitParameter("productChanged");
        readDetailsProductServlet = context.getInitParameter("readDetailsProductServlet");
        informacionesDetalladasProducts = context.getInitParameter("informacionesDetalladasProducts");
        addProductToCartServlet= context.getInitParameter("addProductToCartServlet");
        loginform = context.getInitParameter("loginform");
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
